<?php

/**
 * Controller_student
 */

class Controller_change extends MyApp {

	public function __construct()
	{
		parent::__construct();
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->record = YOU::loadModel('record');
		$this->model->user   = YOU::loadModel('user');
		$this->model->staff   = YOU::loadModel('staff');
	}

	/** 首页 **/
	public function actionIndex()
	{

	}
	/** 人事变动记录列表() **/
	public function actionList ()
	{
		// 进入教师，外聘的入口
		$personnel_type = $this->get->personnel_type;
		// 查询条件
		$where = " ";
		$staff_name = $this->get->staff_name;
		$from = $this->get->from;
		$to = $this->get->to;
		if(!empty($staff_name)){
			$where .= " AND staff_name LIKE '{$staff_name}%' ";
			$this->tpl->assign("staff_name", $staff_name);
		}
		if(!empty($from)){
			$where .= " AND start_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		if(!empty($to)){
			$where .= " AND start_time <= '{$to}' ";
			$this->tpl->assign("to", $to);
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_personnel` WHERE personnel_type ='{$personnel_type}' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_personnel` WHERE personnel_type ='{$personnel_type}' {$where} ORDER BY personnel_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($staff_lists as $key=>$value){
			// 在职标记
			$staff_lists[$key]['staff_sign'] = $this->sign_ini[$value['staff_sign']];
		}
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("personnel_type", $personnel_type);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("count", $count);
		$this->tpl->display("change/changenotes.list.tpl.php");
	}
	/** 导出数据 **/
	public function actionOutData()
	{
		$external = $this->get->staff_external;

		$staff_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff` WHERE staff_external = {$external}  ORDER BY staff_id ");

		foreach ($staff_lists as $key => $value) {

			$info = array(
			//编号	姓名	变动事项	日期	源单位	目的单位	在岗信息	备注	操作

			"0" => $value['staff_number'],
			"1" => $value['staff_name'],
			"2" => $value['staff_variation'],
			"3" => $value['update_time'],
			"4" => $value['staff_from'],
			"5" => $value['staff_to'],
			"6" => $this->sign_ini[$value['staff_sign']],
			"7" => $value['staff_remark'],

			);
			$data_lists[$key] = $info;

		}
		//var_dump($data_lists);exit;
		// 模板标题
		$this->model->excel->excel_ini['exchange']['title'] = "外聘变动表";
		//var_dump($this->model->excel->excel_ini['exchange']['col']);exit;
		$this->model->excel->outData("exchange", $data_lists);

		//$this->tpl->display();
	}

	/** 人事变动添加 **/
	public function actionAdd()
	{
		// 进入教室，外聘入口
		$personnel_type = (int)$this->get->personnel_type;
        $this->tpl->assign("personnel_type",$personnel_type);

		$staff_id   	= (int)$this->get->staff_id;
		$staff_data 	= $this->model->staff->getinfo($staff_id);

		if (empty($staff_data)){
			Helper_Client::alert("人员不存在");
		}
		// 在职标记
		$staff_sign = $this->sign_ini;
		//时间日期转换
		$teacher_data['update_time'] = date("Y-m-d", strtotime($teacher_data['update_time']));
		if ($_POST){
			$staff_number    		= $this->post->staff_number;
			$staff_name      		= $this->post->staff_name;
			$personnel_variation 	= $this->post->personnel_variation;
			$start_time     		= $this->post->start_time;
			$personnel_from      	= $this->post->personnel_from;
			$personnel_to        	= $this->post->personnel_to;
			$staff_sign      		= $this->post->staff_sign;
			$personnel_remark    	= $this->post->personnel_remark;

			// 人事变动历史记录
			$info = array(
			"staff_id"     			=> $staff_id,
			"staff_number"     		=> $staff_data['staff_number'],
			"staff_name"       		=> $staff_name,
			"personnel_variation"  	=> $personnel_variation,
			"start_time"      		=> $start_time,
			"personnel_from"    	=> $personnel_from,
			"personnel_to"      	=> $personnel_to,
			"staff_sign"       		=> $staff_sign,
			"personnel_remark"  	=> $personnel_remark,
			"personnel_type"  		=> $personnel_type,
			"status"            	=> 1,
			"create_time"       	=> date("Y-m-d H:i:s"),
			"update_time"       	=> "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_personnel", $info);
            $info2 = array(
                "staff_variation"  	=> $personnel_variation,
                "staff_sign"       		=> $staff_sign,
                "update_time"       	=> "0000-00-00 00:00:00",
            );
            $this->db->do_update("tbl_staff", $info2 ," staff_id='{$staff_id}'");
			// 跳转
			if ($personnel_type == 2) {
				Helper_Client::location("变动成功", "index.php?a=external.list&personnel_type={$personnel_type}");
			}else{
				Helper_Client::location("变动成功", "index.php?a=teacher.list&personnel_type={$personnel_type}");
			}
		}
		$this->tpl->assign("staff_data", $staff_data);

		$this->tpl->display("change/change.add.tpl.php");
	}

	/** 人事变动修改 **/
	public function actionUpdate()
	{
		$staff_id   = (int)$this->get->staff_id;

		$teacher_data = $this->db->fetchRow("SELECT * FROM `tbl_staff` WHERE `staff_id`='{$staff_id}'");
		if (empty($teacher_data)){
			Helper_Client::alert("人员不存在");
		}
		// 在职标记
		$staff_sign = $this->sign_ini;
		//时间日期转换
		$teacher_data['update_time'] = date("Y-m-d", strtotime($teacher_data['update_time']));
		if ($_POST){
			$staff_number    = $this->post->staff_number;
			$staff_name      = $this->post->staff_name;
			$staff_variation = $this->post->staff_variation;
			$update_time     = $this->post->update_time;
			$staff_from      = $this->post->staff_from;
			$staff_to        = $this->post->staff_to;
			$staff_sign      = $this->post->staff_sign;
			$staff_remark    = $this->post->staff_remark;

			// 写入数据库
			$info = array(
			"staff_number"     => $staff_number,
			"staff_name"       => $staff_name,
			"staff_variation"  => $staff_variation,
			"update_time"      => $update_time,
			"staff_from"       => $staff_from,
			"staff_to"         => $staff_to,
			"staff_sign"       => $staff_sign,
			"staff_remark"     => $staff_remark,
			);
			$this->db->do_update("tbl_staff", $info, "staff_id='{$staff_id}'");
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=change.List&staff_id={$staff_id}");
		}
		$this->tpl->assign("teacher_data", $teacher_data);
		$this->tpl->assign("staff_sign", $staff_sign);
		$this->tpl->display("change/change.update.tpl.php");
	}

}

?>